/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package webtest;

import java.awt.FlowLayout;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.net.*;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class UDPServer {

    public static void main(String args[]) throws Exception {
        DatagramSocket serverSocket = new DatagramSocket(9876);
        DatagramSocket socket2 = new DatagramSocket(9877);
        //byte rd = sender.datastream        
        byte[] receiveData = new byte[60000];
        byte[] sendData = new byte[60000];
        byte[] recieveData2 = new byte[60000];

        while (true) {

            //klient1
            DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
            serverSocket.receive(receivePacket);
            //String sentence = new String(receivePacket.getData());
            //System.out.println("RECEIVED: " + sentence);
            System.out.println("Recieved file!");
            InetAddress IPAddress = receivePacket.getAddress();
            int port = receivePacket.getPort();

            //String capitalizedSentence = sentence.toUpperCase();
            //sendData = capitalizedSentence.getBytes();
            //DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
            //serverSocket.send(sendPacket);
            //klient2
            sendData = receiveData;
            DatagramPacket recieve2 = new DatagramPacket(recieveData2, recieveData2.length);
            socket2.receive(recieve2);
            String sentence2 = new String(recieve2.getData());
            System.out.println("Request recieved from client 2: " + sentence2);
            InetAddress address2 = recieve2.getAddress();
            int port2 = recieve2.getPort();
            System.out.println("Recieved file!");
            if (sentence2.contains("Get")) {
                DatagramPacket p = new DatagramPacket(sendData, sendData.length, address2, port2);
                for (int i = 0; i < 10; i++) {
                    socket2.send(p);
                    System.out.println("1");
                }
            }
        }
    }
}
